<template>
  <div id="login-container">
    <div id="loginbox">
      <div id="logo_box">
        <img src="../assets/logo.png" />
      </div>
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginFromRules">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username">
            <i slot="prefix" class="iconfont icon-user"></i>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" show-password>
            <i slot="prefix" class="iconfont icon-pwd"></i>
          </el-input>
        </el-form-item>
        <el-col :push="15">
          <el-button type="primary" @click="login()">登录</el-button>
          <el-button type="info" @click="reset()">重置</el-button>
        </el-col>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: ""
      },
      loginFromRules: {
        username: [
          { required: true, message: "用户名必填", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        password: [{ required: true, message: "密码必填", trigger: "blur" }]
      }
    };
  },
  methods: {
    login() {
      this.$refs.loginFormRef.validate(async valid => {
        if (valid == true) {
          var { data: dt } = await this.$http.post("login", {
            username: this.loginForm.username,
            password: this.loginForm.password
          });

          if (dt.meta.status != 200) {
            return this.$message.error(dt.meta.msg);
          }
          sessionStorage.setItem("token", dt.data.token);
          this.$router.push("/home");
        }
      });
    },
    reset() {
      this.$refs.loginFormRef.resetFields();
    }
  }
};
</script>
<style lang="less" scoped>
#login-container {
  background-color: #2b4b6b;
  height: 100%;
  position: relative;
  #loginbox {
    width: 450px;
    height: 304px;
    background-color: white;
    border-radius: 5px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }
  #logo_box {
    width: 130px;
    height: 130px;
    border-radius: 50%;
    border: 1px solid #eee;
    position: absolute;
    left: 50%;
    top: -50%;
    transform: translate(-50%, 50%);
    background-color: #fff;
    padding: 8px;
    box-shadow: 0px 0px 10px #eee;
    img {
      border-radius: 50%;
      width: 100%;
      height: 100%;
      background-color: #eee;
    }
  }
}
.el-form {
  position: absolute;
  bottom: 0;
  padding: 20px;
  width: 100%;
  box-sizing: border-box;
}
.el-button:nth-child(2) {
  margin-left: 15px;
}
</style>
